using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
using UnityEngine.Events;
using System.Runtime.InteropServices;

[CreateAssetMenu(fileName = "TileData",menuName = "Custom/TileData")]
public class TileData:TileBase, ParticleCreator {

	[SerializeField] TileBase _tile;
	public TileBase tile { get { return _tile; } }
	[SerializeField] TileBase _tileWall;
	public TileBase tileWall { get { return _tileWall; } }
	[SerializeField] TileBase _tileMinimap;
	public TileBase tileMinimap { get { return _tileMinimap; } }
	[SerializeField] Sprite _spriteEditor;
	public Sprite spriteEditor { get { return _spriteEditor; } }
	[SerializeField] bool _isSolid;
	public bool isSolid { get { return _isSolid; } }
	[SerializeField] bool _isPassable;
	public bool isPassable { get { return _isPassable; } }
	[SerializeField] RoleInRoom _roleInRoom;
	public RoleInRoom roleInRoom { get { return _roleInRoom; } }
	[field: SerializeField] public TileData afterDestroy { get; private set; }

	[SerializeField] GameObject particle;
	[SerializeField] AudioClip soundHit;
	GameObject ParticleCreator.getParticle => particle;
	AudioClip ParticleCreator.getSound => soundHit;
	[field: SerializeField] public float hpMax { get; private set; }
	[field: SerializeField] public float hpRegenRate { get; private set; }
	[field: SerializeField] public int armorLevel { get; private set; }

	public TileData() {
		tileData.Add(this);
	}

	public void OnEnable() {
		nameToData.Add(name,this);
	}

	public static readonly HashSet<TileData> tileData = new HashSet<TileData>();
	public static readonly Dictionary<string,TileData> nameToData = new Dictionary<string,TileData>();
	//public static readonly Dictionary<TileBase,TileData> tileToData = new Dictionary<TileBase,TileData>();
	//public static readonly Dictionary<TileBase,TileData> tileWallToData = new Dictionary<TileBase,TileData>();
	//public static readonly Dictionary<TileBase,TileData> tileMinimapToData = new Dictionary<TileBase,TileData>();
	//public static readonly Dictionary<TileBase,TileData> tileEditorToData = new Dictionary<TileBase,TileData>();

	public static void ChangeTile(Vector2Int position,TileData before,TileData after) {
		if(before) before.OnTileRemove(position,after);
	}

	virtual protected void OnTileRemove(Vector2Int position,TileData other) {

	}

	[System.Serializable]
	public enum RoleInRoom {
		Inside,
		Wall,
		Outside
	}

	public override void GetTileData(Vector3Int position,ITilemap tilemap,ref UnityEngine.Tilemaps.TileData tileData) {
		base.GetTileData(position,tilemap,ref tileData);
		tileData.sprite=spriteEditor;
	}

	public override bool StartUp(Vector3Int position,ITilemap tilemap,GameObject go) {
		try {
			TilemapController.OnSetTile(position,this);
		} catch(System.Exception) { }
		return base.StartUp(position,tilemap,go);
	}

	public virtual void OnDamage(Vector2Int position,DamageStats damage,ref TileHpState instance) {
		int levelDifference = armorLevel-damage.destructionLevel;
		float damageMultiplier = levelDifference>0 ? Mathf.Pow(0.25f,levelDifference) : 1;
		instance.hpCurrent-=damage.amount*damageMultiplier;
	}

}